package avl;

import javax.accessibility.AccessibleValue;

public class AVLTreeDemo {
    public static void main(String[] args) {
//        int[] arr = {4, 3, 6, 5, 7, 8};
//        int[] arr = {10, 12, 8, 9, 7, 6};
        int[] arr = {10, 11, 7, 6, 8, 9};
        AVLTree avlTree = new AVLTree();
        for (int i = 0; i < arr.length; i++) {
            avlTree.add(new Node(arr[i]));
        }
        System.out.println("中序遍历");
        avlTree.infixOrder();
        System.out.println("树的高度=" + avlTree.getRoot().height());
        System.out.println("树的左子树的高度=" + avlTree.getRoot().leftHeight());
        System.out.println("树的右子树的高度=" + avlTree.getRoot().rightHeight());
        System.out.println("当前跟结点=" + avlTree.getRoot());
        System.out.println("当前跟结点的左结点=" + avlTree.getRoot().left);
        System.out.println("当前跟结点的右节点=" + avlTree.getRoot().right);
    }
}

class AVLTree {
    private Node root;


    public Node getRoot() {
        return root;
    }

    //查找要删除的结点
    public Node search(int value) {
        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }

    //查找要删除结点的父节点
    public Node searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }

    //删除结点
    public void delNode(int value) {
        if (root == null) {
            return;
        }
        //1.需要先去找到要删除的结点 targetNode
        Node targetNode = search(value);
        if (targetNode == null) {
            return;
        }
        //如果我们发现当前这颗二排序树只有一个结点
        if (root.left == null && root.right == null) {
            root = null;
            return;
        }

        //去找到targetNode的父节点
        Node parent = searchParent(value);

        //如果要删除的结点是叶子结点
        if (targetNode.left == null && targetNode.right == null) {
            //判断 targeNode 是父节点的左子节点还是右子结点
            if (parent.left != null && parent.left.valuel == value) {
                parent.left = null;
            } else if (parent.right != null && parent.right.valuel == value) {
                parent.right = null;
            }
        } else if (targetNode.left != null && targetNode.right != null) {
            //删除有两颗子树的结点
            int minVal = delRightTreeMin(targetNode.right);
            targetNode.valuel = minVal;

        } else {
            //删除只有一颗子树的结点
            if (targetNode.left != null) {
                if (parent != null) {
                    //如果targetNode 是 parent 的左子节点
                    if (parent.left.valuel == value) {
                        parent.left = targetNode.left;
                    } else {
                        parent.right = targetNode.left;
                    }
                } else {
                    root = targetNode.left;
                }
            } else {
                if (parent != null) {
                    if (parent.left.valuel == value) {
                        parent.left = targetNode.right;
                    } else {
                        parent.right = targetNode.right;
                    }
                } else {
                    root = targetNode.right;
                }
            }
        }
    }

    /**
     * @param node 传入的结点
     * @return 返回以node为跟结点的儿茶排序树的最小结点的值
     * 删除node为结点的二叉排序树的最小结点
     */
    public int delRightTreeMin(Node node) {
        Node target = node;
        while (target.left != null) {
            target = target.left;
        }
        delNode(target.valuel);
        return target.valuel;
    }

    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        } else {
            System.out.println("二叉排序树为空");
        }
    }
}

class Node {
    int valuel;
    Node left;
    Node right;

    //右旋
    public void rigthRotate() {
        Node newNode = new Node(valuel);
        newNode.right = right;
        newNode.left = left.right;
        valuel = left.valuel;
        left = left.left;
        right = newNode;
    }

    //左旋转方法
    private void leftRotate() {
        Node newNode = new Node(valuel);

        //把新的结点左子树设置为当前结点的左子树
        newNode.left = left;
        //把新的结点的右子树设置成你过去结点的右子树的左子树
        newNode.right = right.left;
        //把当前结点的值替换成右子结点的值
        valuel = right.valuel;
        //把当前结点的右子树设置为当前结点右子树的右子树
        right = right.right;
        //把当前结点的左子树（左子节点）设置为新的结点
        left = newNode;
    }


    //返回左子树的高度
    public int leftHeight() {
        if (left == null) {
            return 0;
        }
        return left.height();
    }

    //返回左子树的高度
    public int rightHeight() {
        if (right == null) {
            return 0;
        }
        return right.height();
    }


    //返回以该节点为跟结点的高度
    public int height() {
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }


    public Node(int valuel) {
        this.valuel = valuel;
    }
    //查找要删除结点的父节点

    /**
     * @param valuel 要查找的结点的值
     * @return 返回的是删除的结点的父节点，如果没有就返回null
     */
    public Node searchParent(int valuel) {
        if ((this.left != null && this.left.valuel == valuel)
                || (this.right != null && this.right.valuel == valuel)) {
            return this;
        } else {
            if (valuel < this.valuel && this.left != null) {
                return this.left.searchParent(valuel);
            } else if (valuel >= this.valuel && this.right != null) {
                return this.right.searchParent(valuel);
            } else {
                return null;
            }
        }
    }


    //查找要删除的结点
    public Node search(int value) {
        if (value == this.valuel) {
            return this;
        } else if (value < this.valuel) {
            if (this.left == null) {
                return null;
            }
            return this.left.search(value);
        } else {
            if (this.right == null) {
                return null;
            }
            return this.right.search(value);
        }
    }

    @Override
    public String toString() {
        return "Node{" +
                "valuel=" + valuel +
                '}';
    }

    //中序遍历
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }


    //添加结点的方法
    //递归的形式添加结点，注意要满足二差排序树的要求
    public void add(Node node) {
        if (node == null) {
            return;
        }
        if (node.valuel < this.valuel) {
            if (this.left == null) {
                this.left = node;
            } else {
                this.left.add(node);
            }
        } else {
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }
        }

        //当添加完一个结点后，如果 ： 右子树高度-左子树高度 >1,左旋转
        if (rightHeight() - leftHeight() > 1) {
            //如果他的右子树的左子树的高度打印他的右子树的右子树的高度
            if (right != null && right.leftHeight() > right.rightHeight()) {
                right.rigthRotate();
                leftRotate();
            } else {
                leftRotate();
            }
            return;
        }

        if (leftHeight() - rightHeight() > 1) {
            //如果他的左子树的右子树的高度大于他的左子树的高度
            if (left != null && left.rightHeight() > left.leftHeight()) {
                left.leftRotate();
                rigthRotate();
            } else {
                rigthRotate();
            }
        }
    }
}
